package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import com.aliyun.gts.bpaas.middleware.mq.api.model.MqPayload;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.MqMessageContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.ProcessItem;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.MqProcessTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.MqProducer;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.SpringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpTerminateProcessInstanceDto;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowProcessInstanceTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowProcessInstanceTraceRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTerminateProcessRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * 　　* @description: 流程实例终止
 * 　　* @author lzb
 * 　　* @date 2021/11/29 16:58
 */
@Slf4j
@Repository
public class FlowTerminateProcessRepositoryImpl implements FlowTerminateProcessRepository {

    @Autowired
    private FlowProcessInstanceTraceRepository flowProcessInstanceTraceRepository;

    @Override
    public void flowTerminateProcessSendMessage(HttpTerminateProcessInstanceDto httpTerminateProcessInstanceDto) {
        String processInstanceId = httpTerminateProcessInstanceDto.getProcessInstanceId();
        FlowProcessInstanceTraceVo flowProcessInstanceTraceVo = flowProcessInstanceTraceRepository.getProcessInstanceTraceByProcessInstanceId(processInstanceId);
        try {
            ProcessItem processItem = new ProcessItem();
            if (Objects.nonNull(httpTerminateProcessInstanceDto.getParticipantId())) {
                processItem.setParticipantId(httpTerminateProcessInstanceDto.getParticipantId());//处理人ID
            }
            if (Objects.nonNull(httpTerminateProcessInstanceDto.getParticipantName())) {
                processItem.setParticipantName(httpTerminateProcessInstanceDto.getParticipantName());//处理人名称
            }
            processItem.setProcessInstanceName(flowProcessInstanceTraceVo.getModelName());//流程名称
            processItem.setProcessInstanceKey(flowProcessInstanceTraceVo.getModelKey());//流程编码
            processItem.setProcessInstanceId(flowProcessInstanceTraceVo.getProcessInstanceId());//流程实例id
            processItem.setProcessInstanceVersion(String.valueOf(flowProcessInstanceTraceVo.getVersion()));
            processItem.setApplyUserId(flowProcessInstanceTraceVo.getStarterId());
            processItem.setApplyUserName(flowProcessInstanceTraceVo.getStarterName());
            MqMessageContext messageContext = MqMessageContext.builder().payload(MqPayload.make(processItem))
                    .tag(MqProcessTypeEnum.TERMINATE.getType())
                    .messageKey(httpTerminateProcessInstanceDto.getWorkItemId())
                    .build();
            //这里可以走异步，前面拼接参数还是得同步
            doSendMessage(messageContext);
        } catch (Throwable e) {
            log.error("mq信息未配置:", e);
        }
    }

    private void doSendMessage(MqMessageContext messageContext) {
        MqProducer producer = SpringUtils.getBean(MqProducer.class);
        producer.sendMessage(messageContext);
    }
}
